package com.medica.ledproject.controller;

/**
 * @author xiaona
 * @date 2024/5/30
 * @description TODO
 */
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.gson.JsonObject;
import com.medica.ledproject.common.ResponseEntity;
import com.medica.ledproject.socke.SocketHandler;
import com.medica.ledproject.socke.HrsUtil;
import io.netty.channel.Channel;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.net.InetSocketAddress;
import java.util.Arrays;

@CrossOrigin
@RestController
@RequestMapping({"kongzhi"})
@Slf4j
public class NettyController
{
    @Operation(summary = "合闸")
    @ApiOperationSupport(order = 9)
    @PostMapping(value = "/kai")
    @ResponseBody
    public ResponseEntity kai()
    {
        String m="01 05 00 01 FF 00 DD FA";
        byte[] s= HrsUtil.hexStringToByteArray(m);
        for (Channel client : SocketHandler.clients) {
            System.out.println(222222);
            client.writeAndFlush(s);
        }
        if(SocketHandler.clients.size()>0){
            return ResponseEntity.ok();

        }else{
            return  ResponseEntity.failed("设备没有连接");
        }
    }
    @Operation(summary = "分闸")
    @ApiOperationSupport(order = 9)
    @PostMapping(value = "/guan")
    public ResponseEntity guan() {
        String m="01 05 00 02 FF 00 2D FA";
        byte[] s= HrsUtil.hexStringToByteArray(m);
        for (Channel client : SocketHandler.clients) {
            System.out.println(111111);
            client.writeAndFlush(s);
        }
        if(SocketHandler.clients.size()>0){
            return ResponseEntity.ok();

        }else{
            return  ResponseEntity.failed("设备没有连接");
        }
    }

    @Operation(summary = "分闸/合闸")
    @ApiOperationSupport(order = 9)
    @PostMapping(value = "/turnOnOrOff")
    public ResponseEntity turnOnOrOff(String[] ips,int type) {
        log.info("请求参数为：ips="+ips.toString()+",type="+type);
        //默认分闸,断电（关）
        String m="01 05 00 02 FF 00 2D FA";
        if(type==2){
            //合闸  通电（开）
            m="01 05 00 01 FF 00 DD FA";
        }
        try {
            byte[] s= HrsUtil.hexStringToByteArray(m.toString());
            for (Channel client : SocketHandler.clients) {
                String clientIp = ((InetSocketAddress) client.remoteAddress()).getHostString();
                log.info("当前客户端的IP为："+clientIp);
                if(ips==null||ips.length==0){
                    log.info("没有指定IP,默认全部执行");
                    client.writeAndFlush(s);
                }else {
                    if (Arrays.asList(ips).contains(clientIp)) {
                        log.info("当前客户端的IP在指定IP中，执行操作");
                        client.writeAndFlush(s);
                    }else{
                        log.info("当前客户端的IP不在指定IP中，不执行操作");
                    }
                }

            }
            if(SocketHandler.clients.size()>0){
                return ResponseEntity.ok();

            }else{
                return  ResponseEntity.failed("设备没有连接");
            }
        }catch (Exception e){
            return  ResponseEntity.failed(e.getMessage());
        }

    }
}